package com.uinnova.product.eam.mvc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.binary.framework.bean.annotation.Comment;
import com.binary.framework.web.RemoteResult;
import com.binary.jdbc.Page;
import com.uinnova.product.eam.model.cj.vo.DiagramPlanVO;
import com.uinnova.product.eam.model.vo.CheckAssertEditReq;
import com.uinnova.product.eam.model.vo.CheckAssertEditRes;
import com.uinnova.product.eam.service.BmDiagramSvc;
import com.uinnova.product.eam.service.GTDiagramService;
import com.uinnova.product.eam.vo.AssertRange;
import com.uinnova.product.eam.vo.AssertRangeReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Comment("视图")
@RestController
@RequestMapping("/diagram")
public class DiagramController {

    @Autowired
    private BmDiagramSvc diagramSvc;
    @Autowired
    private GTDiagramService gtDiagramService;

    /**
     * 资产编辑权限
     * @param req
     * @return
     */
    @PostMapping("/assert/editAuth")
    public RemoteResult checkAssertEditAuth(@RequestBody CheckAssertEditReq req) {
        List<CheckAssertEditRes> list = diagramSvc.checkAssertEditAuth(req, false);
        return new RemoteResult(list);
    }

    /**
     * 基于制品创建的视图个数
     * @param artifactId
     * @return
     */
    @GetMapping("/artifact/useCount/{artifactId}")
    public RemoteResult artifactUseCount(@PathVariable Long artifactId) {
        long count = gtDiagramService.artifactUseCount(artifactId);
        return new RemoteResult(count);
    }

    @PostMapping("/release/versions")
    public RemoteResult queryDiagramReleaseVersion(@RequestBody String body) {
        JSONObject jsonObject = JSON.parseObject(body);
        String diagramId = jsonObject.getString("diagramId");
        List<Integer> versions = gtDiagramService.queryDiagramReleaseVersion(diagramId);
        return new RemoteResult(versions);
    }

    @PostMapping("/assertRanges")
    public RemoteResult assertRanges(@RequestBody AssertRangeReq req) {
        Map<String, List<AssertRange>> data = gtDiagramService.assertRanges(req);
        return new RemoteResult(data);
    }

    @PostMapping("/getAllDiagrams")
    public RemoteResult getAllDiagrams(@RequestBody String body) {
        JSONObject jsonObject = JSON.parseObject(body);
        Page<DiagramPlanVO> result = gtDiagramService.getAllDiagrams(jsonObject);
        return new RemoteResult(result);
    }
}
